//
//  MLAPayCodeVC.m
//  MagicLampApp
//
//  Created by Zheng on 2019/4/23.
//  Copyright © 2019 zheng.magic.lamp.app. All rights reserved.
//

#import "MLAPayCodeVC.h"
#import "MLAPaySuccessVC.h"
#import "MLAScanResultStatusVC.h"
@interface MLAPayCodeVC ()

@end

@implementation MLAPayCodeVC {
    UIImageView *qrCodeImgv;
    UIButton *wxBtn;
    UIButton *aliBtn;
    NSString *orderId;
    
    
        dispatch_source_t timer;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
        if (timer) {
            dispatch_source_cancel(timer);
            timer = nil;
        }
    
}

- (void)loadData {
    
    if (timer) {
        dispatch_source_cancel(timer);
        timer = nil;
    }
    
    NSString *type = wxBtn.selected ? @"1": @"2";
    NSDictionary *param = @{@"type":type,
                            @"amount":[NSString stringWithFormat:@"%ld",(NSInteger)(_moneyStr.doubleValue*100)]
                            };

    [MLAHttpManage postHostType:MLAHttpHostType_Diancan api:kShopPayPayCode params:param success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        [self->qrCodeImgv sd_setImageWithURL:[NSURL URLWithString:response[@"code_url"]]];
        self->orderId = response[@"order_number"];
        [self startTimeOut];
    } failure:^{
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configTitle:@"二维码收款"];
    
    self.view.backgroundColor = CX_GetColorBlue();
    
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.right.offset(-15);
        make.top.equalTo(self.titleView.mas_bottom).offset(15);
        make.height.equalTo(bgView.mas_width).multipliedBy(1.338);
    }];
    
    UIImageView *iconImgv= [[UIImageView alloc] init];
    iconImgv.image = [UIImage imageNamed:@"收款"];
    [self.view addSubview:iconImgv];
    [iconImgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(39);
        make.width.height.offset(25);
        make.top.equalTo(bgView).offset(CX_GetXScale(15));
    }];
    
    UILabel *titleLbl = [[UILabel alloc] init];
    titleLbl.text = @"二维码收款";
    titleLbl.textColor = CX_GetColorBlue();
    titleLbl.font = CX_GetNormalFont(18);
    [self.view addSubview:titleLbl];
    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImgv.mas_right).offset(12);
        make.centerY.equalTo(iconImgv);
    }];
    
    UILabel *title2Lbl = [[UILabel alloc] init];
    title2Lbl.text = @"扫二维码向我付钱";
    title2Lbl.textColor = CX_GetColorHex(0x4A4A4A, 1);
    title2Lbl.font = CX_GetNormalFont(13);
    [self.view addSubview:title2Lbl];
    [title2Lbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLbl.mas_bottom).offset(CX_GetXScale(32));
        make.centerX.offset(0);
    }];
    
    qrCodeImgv = [[UIImageView alloc] init];
    qrCodeImgv.backgroundColor = CX_GetColorLine();
    [self.view addSubview:qrCodeImgv];
    [qrCodeImgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(CX_GetXScale(190));
        make.centerX.offset(0);
        make.top.equalTo(title2Lbl.mas_bottom).offset(CX_GetXScale(10));
    }];
 
    UILabel *moneyLbl = [[UILabel alloc] init];
    moneyLbl.font = CX_GetNormalFont(29);
    moneyLbl.textAlignment = NSTextAlignmentCenter;
    moneyLbl.text = [NSString stringWithFormat:@"¥ %@",_moneyStr];
    [self.view addSubview:moneyLbl];
    [moneyLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(bgView);
        make.top.equalTo(self->qrCodeImgv.mas_bottom).offset(CX_GetXScale(10));
    }];
 
    UILabel *typeTitleLbl = [[UILabel alloc] init];
    typeTitleLbl.font = CX_GetNormalFont(12);
    typeTitleLbl.text = @"支付方式";
    [self.view addSubview:typeTitleLbl];
    [typeTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(50);
        make.top.equalTo(self->qrCodeImgv.mas_bottom).offset(CX_GetXScale(60));
    }];
    
    wxBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    wxBtn.titleLabel.font = CX_GetNormalFont(12);
    wxBtn.selected = YES;
    [wxBtn setImage:[UIImage imageNamed:@"对号"] forState:UIControlStateNormal];
    [wxBtn setImage:[UIImage imageNamed:@"对号1"] forState:UIControlStateSelected];
    [wxBtn setTitle:@"  微信支付" forState:UIControlStateNormal];
    [wxBtn setTitleColor:CX_GetColorTitle() forState:UIControlStateNormal];
    [wxBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:wxBtn];
    [wxBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(typeTitleLbl).offset(-10);
        make.top.equalTo(typeTitleLbl.mas_bottom).offset(10);
        make.width.offset(90);
        make.height.offset(25);
    }];
    
    
    aliBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    aliBtn.titleLabel.font = CX_GetNormalFont(12);
    [aliBtn setImage:[UIImage imageNamed:@"对号"] forState:UIControlStateNormal];
    [aliBtn setImage:[UIImage imageNamed:@"对号1"] forState:UIControlStateSelected];
    [aliBtn setTitle:@"  支付宝支付" forState:UIControlStateNormal];
    [aliBtn setTitleColor:CX_GetColorTitle() forState:UIControlStateNormal];
    [aliBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:aliBtn];
    [aliBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(typeTitleLbl).offset(-9);
        make.top.equalTo(self->wxBtn.mas_bottom).offset(5);
        make.width.offset(100);
        make.height.offset(25);
    }];

    [self loadData];
}

- (void)btnAction:(UIButton *)sender {
    
    if (sender == wxBtn) {
        wxBtn.selected = YES;
        aliBtn.selected = NO;
    } else {
        wxBtn.selected = NO;
        aliBtn.selected = YES;
    }
    
    [self loadData];
}

- (void)startTimeOut {
    __block int timeout = 60*60;

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(timer,dispatch_walltime(NULL, 0), 2.0*NSEC_PER_SEC, 0);

    dispatch_source_set_event_handler(timer, ^{
        if(timeout <= 0) {
            dispatch_source_cancel(self->timer);
            dispatch_async(dispatch_get_main_queue(), ^{

            });
        } else {

            dispatch_async(dispatch_get_main_queue(), ^{
                [self getPaySuccess];
            });
            timeout--;
        }
    });
    dispatch_resume(timer);
}

- (void)getPaySuccess {
    [MLAHttpManage postHostType:MLAHttpHostType_Diancan api:kShopPayGetPayCode params:@{@"order_number":orderId} success:^(id  _Nonnull response, NSString * _Nonnull msg) {

        if ([response[@"status"] integerValue] == 2) {
            dispatch_source_cancel(self->timer);
            self->timer = nil;
            [self.navigationController popViewControllerAnimated:YES];
            MLAScanResultStatusVC *srsVC = [[MLAScanResultStatusVC alloc] init];
            srsVC.isSuccess = YES;
            [self.navigationController pushViewController:srsVC animated:YES];
        }
        
    } failure:^{

    }];

}


@end
